package com.meshnetics.gb.stack.mac.primitives;

import com.meshnetics.gb.stack.core.event.StackEventType;
import com.meshnetics.gb.stack.core.event.StackIndication;
import com.meshnetics.gb.stack.core.layer.StackLayer;
import com.meshnetics.gb.stack.mac.RadioMACStackLayer;
import com.meshnetics.gb.stack.mac.type.SyncLossReason;
import com.meshnetics.gb.stack.mac.type.TypeSyncLossReason;
import com.meshnetics.gb.type.impl.ByteUtils;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class MACSyncLossIndication extends StackIndication {
    public static final byte CODE = 0x60;

    private final SyncLossReason lossReason;

    public MACSyncLossIndication(ByteBuffer bb, RadioMACStackLayer layer) {
        super("syncLossIndication", StackEventType.LME_TYPE, null, layer);
        ByteUtils.ensureNotNull(bb);
        ByteUtils.ensureCapacity(bb, 1);
        this.lossReason = (SyncLossReason) TypeSyncLossReason.TYPE.deserialize(bb);
        if (bb.remaining() > 0) {
            throw new IllegalArgumentException("unexpected content of ByteBuffer, " + bb.remaining() + " bytes left");
        }
    }

    public MACSyncLossIndication(SyncLossReason lossReason) {
        this(lossReason, null);
    }

    public MACSyncLossIndication(SyncLossReason lossReason, StackLayer source) {
        super("syncLossIndication", StackEventType.LME_TYPE, null, source);
        if (lossReason == null) {
            throw new NullPointerException("lossReason is null");
        }
        this.lossReason = lossReason;
    }

    public SyncLossReason getLossReason() {
        return lossReason;
    }

    public String toString() {
        return getName() + '(' + lossReason + ')';
    }
}
